package br.com.ppc.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.com.ppc.modelo.Subfamilia;

public class SubFamiliaDAO extends DAO<Subfamilia> {
private Logger logger = Logger.getLogger(SubFamiliaDAO.class);
	
	public SubFamiliaDAO(Session session, Class<?> classe) {
		super(session, classe);
		// TODO Auto-generated constructor stub
	}
	
	public Subfamilia pesquisaSubfamiliaById(Long idSubfamilia) {
		logger.info("pesquisaSubfamiliaById : " + idSubfamilia);
		return (Subfamilia) session.load(Subfamilia.class, idSubfamilia);
	}
	
	public Subfamilia pesquisaSubfamiliaByNome(String nome) {
		logger.info("pesquisaSubfamiliaByNome : " + nome);
		Criteria c = session.createCriteria(Subfamilia.class);
		c.add(Restrictions.ilike("nome", "%" + nome + "%"));

		return (Subfamilia)c.uniqueResult();
	}

	
	@SuppressWarnings("unchecked")
	public List<Subfamilia> pesquisaSubfamilias(String nome){
		Criteria c = session.createCriteria(Subfamilia.class);
		c.add(Restrictions.ilike("nome", "%" + nome + "%"));
		c.addOrder(Order.asc("nome"));
		
		return c.list();
	}
	
	public Subfamilia buscaSubfamilia(Long id){
		Query q = session.createQuery("select p from " + Subfamilia.class.getName() + " as p where p.id like :id");
		
		q.setParameter("id", id);
		
		return (Subfamilia)q.uniqueResult();
	}
}
